
#########################################################################

# Copyright (C) Flavio Piccinelli 2012 <flavio.picci@gmail.com>

#########################################################################

# Utils.rb
# This file is part of "AriaAperta".

# AriaAperta is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# AriaAperta is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with AriaAperta.  If not, see <http://www.gnu.org/licenses/>.

#########################################################################
#########################################################################


#########################################################################
# Required libraries
#########################################################################
require "lib/const"
require "lib/Aria"
require "lib/MySqLite"
#########################################################################

include MySqLite

module Utils

	def parse_req(req_str, list_symb, range_symb)
		resp_arr = Array.new
		tmp_arr = req_str.split(list_symb)
		if range_symb.nil?
			resp_arr = tmp_arr
		else
			tmp_arr.each do |list_char|
				range_arr = list_char.split(range_symb)
				if range_arr.length == 1
					resp_arr << list_char.to_i
				else
					for i in range_arr[0].to_i..range_arr[1].to_i
						resp_arr << i
					end
				end
			end
			return resp_arr
		end
	end

	def create_tmpl(img, credentials)

		img_id   = img[0]
		img_name = img[1]
		img_boot = img[2]

		case img_boot
		when 0
			img_boot   = "hd"
			img_type   = "disk"
			img_target = "hda"
		when 1
			img_type = img_boot = "cdrom"
			img_target = "hdc"
		end

		passwd = credentials.split(":")[1]

		vm_id = MySqLite::get_vm_id()
		vm_id += 1
		vnc_port = 5900 + vm_id
		udp_port = 30000 + vm_id

# 		puts "vm #{vm_id}"
# 		puts "vnc port: #{vnc_port}, udp port: #{udp_port}"

		vm_tmpl = "
			NAME   = \"#{img_name}-#{vm_id}\"
			CPU    = 0.5
			MEMORY = 128
			OS   = [ BOOT = #{img_boot} ]
			DISK = [
				IMAGE_ID = #{img_id},
				TARGET   = #{img_target},
				CLONE    = #{CLONE},
				READONLY = yes
			]
			GRAPHICS = [
				TYPE = vnc,
				PORT = \"#{vnc_port}\",
				PASSWD = \"#{passwd}\"
			]
			RAW = [
				TYPE = \"kvm\",
				DATA = \"
					<features>
						<pae/>
						<acpi/>
						<apic/>
					</features>
					<qemu:commandline>
						<qemu:arg value='-netdev'/>
						<qemu:arg value='user,hostfwd=udp::#{udp_port}-:50000,id=hostnet#{vm_id}'/>
						<qemu:arg value='-device'/>
						<qemu:arg value='ne2k_pci,netdev=hostnet#{vm_id},id=net#{vm_id}'/>
					</qemu:commandline>
				\"
			]
		"

		return vm_tmpl
	end

	def select_hosts()
		
		hosts_hash = Hash.new
		hosts_arr = Aria::avail_hosts
		hosts_stri = "\nEOL"

		if hosts_arr.length == 0
			hosts_hash = 0
			hosts_stri = "Sorry, no available hosts at the moment, try later.\nBye...\nEOF"
		else
			hosts_arr = hosts_arr.sort{|k,v| v[1]<=>k[1]}
			HOST_NUM.times do |i|
				if !hosts_arr[i].nil?
					hosts_hash["#{hosts_arr[i][0]}"] = hosts_arr[i][2]
					hosts_stri = hosts_arr[i][0] + "+" + hosts_stri
				else
					break
				end
			end
		end

		return hosts_hash, hosts_stri
	end

	def authenticate_user(username, password)

		ret_var = false
		passwd = MySSL::get_encr_passwd(username, password)
		users = MySqLite::get_users
		users.each do |user|
			if user[0] == username && user[1] == passwd
				ret_var = true
				break
			end
		end
		return ret_var
	end

	def do_ack(username)
		ret_var = -1
		users = MySqLite::get_users
		users.each do |user|
			if user[0] == username
				ret_var = MySqLite::update_ack(username, Time.now.to_i)
				break
			end
		end
		return ret_var
	end

	def check_ack()
		users_arr = Array.new
		users = MySqLite::get_users
		time_out = Time.at(TIME_OUT)
		users.each do |user|
			if !user[2].nil? && (Time.now - user[2]) > time_out
				puts "#{user[0]} no ack removing vms.."
				users_arr << user[0]
				MySqLite::update_ack(user[0], nil)
			end
		end
		return users_arr
	end

end
